package com.xinzhi.savingpot.rminfo.service.impl;

import com.xinzhi.savingpot.rminfo.entity.Limitinfo;
import com.xinzhi.savingpot.rminfo.entity.Recordinfo;
import com.xinzhi.savingpot.rminfo.entity.Rulesinfo;
import com.xinzhi.savingpot.rminfo.mapper.IRecordinfoMapper;
import com.xinzhi.savingpot.rminfo.mapper.IRmInfoMapper;
import com.xinzhi.savingpot.rminfo.mapper.ITitleMapper;
import com.xinzhi.savingpot.rminfo.pojo.TitleVo;
import com.xinzhi.savingpot.rminfo.service.ITitleService;
import com.xinzhi.savingpot.userinfo.entity.UserInfo;
import com.xinzhi.savingpot.util.BaseResponse;
import com.xinzhi.savingpot.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class TitleImpl implements ITitleService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ITitleMapper iTitleMapper;
 

    @Override
    public BaseResponse addRecordinfo(TitleVo ti) {
        Recordinfo re = new Recordinfo();
        if (re!=null){
            UserInfo useridByid = iTitleMapper.getUseridByid(ti.getFkuserid());
            Integer shoppoints = useridByid.getShoppoints();
            if (useridByid!=null) re.getFkuserid().setUserId(useridByid.getUserId());
            else {
                return new BaseResponse(400,"未找到该用户",null);
            }
            re.getFkcriminalrecordsid().setRulesinfoid(ti.getFkcriminalrecordsid());
            re.getFklimitrecordid().setLimitinfoid(ti.getFklimitrecordid());
            re.setFilmtime(new Date());
            if (ti.getFklimitrecordid()!=null){
                Limitinfo limittheway = iTitleMapper.getLimittheway(ti.getFklimitrecordid());
                if (limittheway!=null){
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Calendar cal = Calendar.getInstance();
                    cal.getTime();
                    cal.add(Calendar.DATE,limittheway.getLimittheway());
                    String endtime = format.format(cal.getTime());
                    Date date = null;
                    try {
                        date = format.parse(endtime);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    re.setEndtime(date);
                    redisUtil.set("Endtime-"+ti.getFkuserid(),date,limittheway.getLimittheway()*24*60*60);
                }else {
                    return new BaseResponse(300,"暂无该条规定！请重新选择");
                }
            }
            Rulesinfo score = iTitleMapper.getscore(ti.getFkcriminalrecordsid());
            re.setPoints(score.getScore());

            int i = iTitleMapper.addRecordinfo(re);

            //修改店铺分
            int userId = useridByid.getUserId();
            int shoppoints1 = shoppoints - score.getScore();
            System.out.println(shoppoints1);
            iTitleMapper.amendRecord(userId,shoppoints1);

            return new BaseResponse(200,"违规裁决成功",i);
        }
        return new BaseResponse(400,"请填写正确信息",null);
    }

}
